package tt.dz.biz.service.card;

import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import tt.dz.biz.dao.card.OuterCardTypeDAO;
import tt.dz.common.DzConstants;
import tt.dz.common.DzConstants.CardBIN;
import tt.dz.common.LogUtil;
import tt.dz.vo.OuterCardTypeVO;

@Service("cardTypeServiceImpl")
public class CardTypeServiceImpl
  implements CardTypeService
{
  @Resource(name="outerCardTypeDAO")
  private OuterCardTypeDAO outerCardTypeDAO;
  
  public OuterCardTypeVO queryOuterCardTypeByCardBin(String cardBin)
    throws Exception
  {
    try
    {
      return this.outerCardTypeDAO.queryOuterCardTypeByCardBin(cardBin);
    }
    catch (Exception e)
    {
      LogUtil.error("通过卡bin查询外部卡类型异常，卡bin为:" + cardBin, e);
      throw e;
    }
  }
  
  public String getCardType(String cardNo)
    throws Exception
  {
    try
    {
      String cardBin = cardNo.substring(0, 3);
      OuterCardTypeVO cardTypeVO = null;
      String cardType = null;
      if (DzConstants.CardBIN.CARDBIN_188.value().equals(cardBin))
      {
        cardType = cardNo.substring(3, 5);
      }
      /*else
      {
        cardTypeVO = queryOuterCardTypeByCardBin(cardBin);
        if (null == cardTypeVO) {
          cardType = null;
        }else{
        	cardType=cardTypeVO.getCardType();
        }
      }*/
      return cardType;
    }
    catch (Exception e)
    {
      LogUtil.error("通过卡号找卡类型异常，卡号为:" + cardNo, e);
      throw e;
    }
  }
  
  public String getAllOuterCardType()
    throws Exception
  {
    try
    {
      List<OuterCardTypeVO> cardTypeList = this.outerCardTypeDAO.queryAllOuterCardType();
      if (CollectionUtils.isNotEmpty(cardTypeList))
      {
        StringBuilder allType = new StringBuilder();
        for (OuterCardTypeVO outerCardTypeVO : cardTypeList) {
          allType.append(",").append(outerCardTypeVO.getCardType());
        }
        allType.deleteCharAt(0);
        return allType.toString();
      }
      return "";
    }
    catch (Exception e)
    {
      LogUtil.error("查找所有外部卡类型异常", e);
      throw e;
    }
  }
}
